package others.swing.p023;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TanChiShe extends JFrame implements ActionListener, KeyListener, Runnable {
    JMenuBar menuBar;
    JMenu youXi, ny, fs, gy;
    JMenuItem ks, tc, zz, fsi;
    JCheckBoxMenuItem jd, yb, kn;
    int length = 6; //长度
    Toolkit toolkit;
    int i, x, y, z, objectX, objectY, object = 0, growth = 0, time;
    int difficult = 2;
    int m[] = new int[50];
    int n[] = new int[50];
    Thread she = null;
    int life = 0;
    int foods = 0;
    int fenshu = 0;

    @Override
    public void run() {
        time = 500;
        for (i = 0; i <= length - 1; i++) {
            m[i] = 90 - i * 10;
            n[i] = 60;
        }
        x = m[0];
        y = n[0];
        z = 4;
        while (she != null) {
            check();
            try { //程序异常处理rrr
                she.sleep(time);
            } catch (Exception ee) {
                System.out.println(z + "");
            }
        }
    }

    public TanChiShe() { //菜单栏的显示
        menuBar = new JMenuBar();//显示菜单
        Container con = getContentPane();
        toolkit = getToolkit();

        youXi = new JMenu("游戏");//定义变量
        ks = new JMenuItem("开始(新)游戏");
        tc = new JMenuItem("退出");

        ny = new JMenu("难易程度");
        jd = new JCheckBoxMenuItem("简单");
        yb = new JCheckBoxMenuItem("一般");
        kn = new JCheckBoxMenuItem("困难");
        fs = new JMenu("积分排行");
        fsi = new JMenuItem("最高记录");
        gy = new JMenu("关于");
        zz = new JMenuItem("关于作者");
        gy.add(zz);//添加游戏作者
        ny.add(jd);//添加游戏程度简单
        ny.add(yb);//添加游戏程度一般
        ny.add(kn);//添加游戏程度困难
        fs.add(fsi);//添加积分排行里的最高纪录
        youXi.add(ks);//添加开始游戏在游戏菜单下
        youXi.add(tc);//添加退出游戏在游戏菜单下
        menuBar.add(youXi);//主游戏菜单下添加游戏菜单
        menuBar.add(ny);//添加难易程度菜单
        menuBar.add(fs);//添加分数菜单
        menuBar.add(gy);//添加关于菜单
        zz.addActionListener(this);//监听
        ks.addActionListener(this);//监听
        tc.addActionListener(this);//监听
        addKeyListener(this);//监听
        fsi.addActionListener(this);//监听
        KeyStroke keyOpen = KeyStroke.getKeyStroke('O', InputEvent.CTRL_DOWN_MASK);//快捷设置键ctrl+o：开始游戏
        ks.setAccelerator(keyOpen);
        KeyStroke keyExit = KeyStroke.getKeyStroke('X', InputEvent.CTRL_DOWN_MASK);//快捷设置键ctrl+x：退出游戏
        tc.setAccelerator(keyExit);
        setJMenuBar(menuBar);
        setTitle("贪吃蛇");//运行界面的标题
        setResizable(false);
        setBounds(300, 200, 400, 400);//画布的大小
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        validate();
        setVisible(true);//设置可见
    }

    public static void main(String args[]) {
        TanChiShe tcs = new TanChiShe();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == ks) { //开始新游戏
            length = 6;
            life = 0;
            foods = 0;
            if (she == null) {
                she = new Thread(this);
                she.start();
            } else if (she != null) {
                she = null;
                she = new Thread(this);
                she.start();
            }
        }
        if (e.getSource() == tc) { //退出
            System.exit(0);
        }
        if (e.getSource() == zz) { //作者
            JOptionPane.showMessageDialog(this, "Jack魏" + "\n\n" + "\n");
        }
        if (e.getSource() == fsi) { //最高纪录
            JOptionPane.showMessageDialog(this, "最高记录为" + fenshu + "");
        }
    }

    public void check() {
        isDead();
        if (she != null) {
            if (growth == 0) {
                reform();
            } else {
                upgrowth();
            }
            if (x == objectX && y == objectY) {
                object = 0;
                growth = 1;
                toolkit.beep();
            }
            if (object == 0) {
                object = 1;
                objectX = (int) Math.floor(Math.random() * 39) * 10;//随机产生
                objectY = (int) Math.floor(Math.random() * 29) * 10 + 50;
            }
            this.repaint();//重新绘制
        }
    }

    void isDead() {
        if (z == 4) {
            x = x + 10;
        } else if (z == 3) {
            x = x - 10;
        } else if (z == 2) {
            y = y + 10;
        } else if (z == 1) {
            y = y - 10;
        }
        if (x < 0 || x > 390 || y < 50 || y > 390) { //判断游戏是否结束
            she = null;
            JOptionPane.showMessageDialog(null, "GAME OVER", "Game Over", JOptionPane.INFORMATION_MESSAGE);
        }
        for (i = 1; i < length; i++) {
            if (m[i] == x && n[i] == y) {
                she = null;
                JOptionPane.showMessageDialog(null, "GAME OVER", "Game Over", JOptionPane.INFORMATION_MESSAGE);
            }
        }

    }

    public void upgrowth() {
        if (length < 50) {
            length++;
        }
        growth--;
        time = time - 10;//时间减10
        reform();
        life += 100;//分数加100
        if (fenshu < life) {
            fenshu = life;
        }
        foods++;//当前吃到的食物加1
    }

    public void reform() {
        for (i = length - 1; i > 0; i--) {
            m[i] = m[i - 1];
            n[i] = n[i - 1];
        }
        if (z == 4) {
            m[0] = m[0] + 10;
        }
        if (z == 3) {
            m[0] = m[0] - 10;
        }
        if (z == 2) {
            n[0] = n[0] + 10;
        }
        if (z == 1) {
            n[0] = n[0] - 10;
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (she != null) {
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                if (z != 2) {
                    z = 1;
                    check();
                }
            } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                if (z != 1) {
                    z = 2;
                    check();
                }
            } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                if (z != 4) {
                    z = 3;
                    check();
                }
            } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                if (z != 3) {
                    z = 4;
                    check();
                }
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.DARK_GRAY);
        g.fillRect(0, 50, 400, 400);
        g.setColor(Color.pink);
        for (i = 0; i <= length - 1; i++) {
            g.fillRect(m[i], n[i], 10, 10);
        }
        g.setColor(Color.green);
        g.fillRect(objectX, objectY, 10, 10);
        g.setColor(Color.black);
        g.drawString("当前      分数" + this.life, 6, 60);
        g.drawString("当前已吃食物数" + this.foods, 6, 72);
    }
}
